package com.kongjian.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kongjian.manage.common.CustomException;
import com.kongjian.manage.common.R;
import com.kongjian.manage.mapper.WaterLevelRecordsMapper;
import com.kongjian.manage.model.dto.NewWaterLevelRecordsDTO;
import com.kongjian.manage.model.dto.UpdateWaterLevelRecordsDTO;
import com.kongjian.manage.model.entity.WaterLevelRecordsEntity;
import com.kongjian.manage.model.query.WaterLevelRecordsQuery;
import com.kongjian.manage.model.vo.WaterLevelRecordsVO;
import com.kongjian.manage.service.IWaterLevelRecordsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author tangjunfeng
 * @Classname WaterLevelRecordsServiceImpl.java
 * @Description 水位记录表service
 * @Date 2023-10-20
 * @Version V1.0
 */
@Service
public class WaterLevelRecordsServiceImpl extends ServiceImpl<WaterLevelRecordsMapper, WaterLevelRecordsEntity> implements IWaterLevelRecordsService {


    @Override
    public R<Page> queryPageWaterLevelRecords(WaterLevelRecordsQuery query) throws Exception {
        //构造分页构造器
        Page pageInfo = new Page(query.getPage(), query.getSize());
        LambdaQueryWrapper<WaterLevelRecordsEntity> lambda = queryWhere(query);

        //执行查询
        Page result = this.page(pageInfo, lambda);
        return R.success(result);
    }

    @Override
    public WaterLevelRecordsVO getByIdWaterLevelRecords(Long id) throws Exception {
        WaterLevelRecordsVO waterLevelRecordsVO = new WaterLevelRecordsVO();
        WaterLevelRecordsEntity waterLevelRecordsEntity = baseMapper.selectById(id);
        if (waterLevelRecordsEntity != null) {
            BeanUtils.copyProperties(waterLevelRecordsEntity, waterLevelRecordsVO);
        }
        return waterLevelRecordsVO;
    }

    @Override
    public void addWaterLevelRecords(NewWaterLevelRecordsDTO waterLevelRecordsDTO) throws Exception {
        WaterLevelRecordsEntity waterLevelRecords = new WaterLevelRecordsEntity();
        BeanUtils.copyProperties(waterLevelRecordsDTO, waterLevelRecords);
        //根据时间查询，有则更新，无则插入
        LambdaQueryWrapper<WaterLevelRecordsEntity> lambda = new LambdaQueryWrapper<>();
        lambda.eq(WaterLevelRecordsEntity::getRecordTime, waterLevelRecordsDTO.getRecordTime());
        WaterLevelRecordsEntity entity = this.baseMapper.selectOne(lambda);
        if (entity == null) {
            waterLevelRecords.setCreateTime(LocalDateTime.now());
            baseMapper.insert(waterLevelRecords);
        } else {
            if (waterLevelRecordsDTO.getWater() != entity.getWater() || waterLevelRecordsDTO.getPower() != entity.getPower() ||
                    waterLevelRecordsDTO.getEcologyflow() != entity.getEcologyflow() || waterLevelRecordsDTO.getDownflow() != entity.getDownflow()) {
                UpdateWaterLevelRecordsDTO dto = new UpdateWaterLevelRecordsDTO();
                BeanUtils.copyProperties(waterLevelRecordsDTO, dto);
                dto.setUpdateTime(LocalDateTime.now());
                modifyWaterLevelRecords(entity.getId(), dto);
            }
        }
    }

    @Override
    public void modifyWaterLevelRecords(Long id, UpdateWaterLevelRecordsDTO waterLevelRecordsDTO) throws Exception {
        WaterLevelRecordsEntity waterLevelRecords = baseMapper.selectById(id);
        if (waterLevelRecords != null) {
            if (waterLevelRecordsDTO.getRecordTime() != null) {
                waterLevelRecords.setRecordTime(waterLevelRecordsDTO.getRecordTime());
            }
            if (waterLevelRecordsDTO.getWaterlevel() != null) {
                waterLevelRecords.setWaterlevel(waterLevelRecordsDTO.getWaterlevel());
            }
            if (waterLevelRecordsDTO.getStorage() != null) {
                waterLevelRecords.setStorage(waterLevelRecordsDTO.getStorage());
            }
            if (waterLevelRecordsDTO.getWarehousing() != null) {
                waterLevelRecords.setWarehousing(waterLevelRecordsDTO.getWarehousing());
            }
            if (waterLevelRecordsDTO.getPower() != null) {
                waterLevelRecords.setPower(waterLevelRecordsDTO.getPower());
            }
            if (waterLevelRecordsDTO.getWater() != null) {
                waterLevelRecords.setWater(waterLevelRecordsDTO.getWater());
            }
            if (waterLevelRecordsDTO.getEcologyflow() != null) {
                waterLevelRecords.setEcologyflow(waterLevelRecordsDTO.getEcologyflow());
            }
            if (waterLevelRecordsDTO.getDownflow() != null) {
                waterLevelRecords.setDownflow(waterLevelRecordsDTO.getDownflow());
            }
            if (waterLevelRecordsDTO.getRecordDate() != null) {
                waterLevelRecords.setRecordDate(waterLevelRecordsDTO.getRecordDate());
            }
            if (waterLevelRecordsDTO.getUpdateTime() != null) {
                waterLevelRecords.setUpdateTime(waterLevelRecordsDTO.getUpdateTime());
            }
            baseMapper.updateById(waterLevelRecords);
        }
    }

    @Override
    public void deleteWaterLevelRecords(Long id) throws Exception {
        WaterLevelRecordsEntity waterLevelRecordsEntity = baseMapper.selectById(id);
        if (waterLevelRecordsEntity == null) {
            throw new CustomException("id:" + id + "没有查到数据");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Integer countWaterLevelRecords(WaterLevelRecordsQuery query) {
        LambdaQueryWrapper<WaterLevelRecordsEntity> lambda = queryWhere(query);
        return Math.toIntExact(baseMapper.selectCount(lambda));
    }

    private LambdaQueryWrapper<WaterLevelRecordsEntity> queryWhere(WaterLevelRecordsQuery query) {
        LambdaQueryWrapper<WaterLevelRecordsEntity> lambda = new LambdaQueryWrapper<>();
        lambda.eq(query.getRecordTime() != null, WaterLevelRecordsEntity::getRecordTime, query.getRecordTime());
        lambda.eq(query.getWaterlevel() != null, WaterLevelRecordsEntity::getWaterlevel, query.getWaterlevel());
        lambda.eq(query.getStorage() != null, WaterLevelRecordsEntity::getStorage, query.getStorage());
        lambda.eq(query.getWarehousing() != null, WaterLevelRecordsEntity::getWarehousing, query.getWarehousing());
        lambda.eq(query.getPower() != null, WaterLevelRecordsEntity::getPower, query.getPower());
        lambda.eq(query.getWater() != null, WaterLevelRecordsEntity::getWater, query.getWater());
        lambda.eq(query.getEcologyflow() != null, WaterLevelRecordsEntity::getEcologyflow, query.getEcologyflow());
        lambda.eq(query.getDownflow() != null, WaterLevelRecordsEntity::getDownflow, query.getDownflow());
        lambda.eq(query.getRecordDate() != null, WaterLevelRecordsEntity::getRecordDate, query.getRecordDate());
        //添加排序条件
        lambda.orderByDesc(WaterLevelRecordsEntity::getUpdateTime);
        return lambda;
    }

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

    }

}
